***File used to compute autocorrelation for Murphy et al. "Chatting at Church: Information Diffusion through Religious Networks"

***Appendix table A19***

clear all
macro drop _all

*CUSTOM DO FILES

*custom installation of spatwmat required. Use command "search spatwmat".

global user "[INSERT PATH HERE]"
cd "${user}"

use factor_analysis.dta
merge 1:1 hhid using decdegcords, gen(merge_gps)
merge 1:m hhid using descriptive_data, gen(merge_indv)
local x HomeCordlongdegdec
local y HomeCordlatdegdec

bysort hhid: gen n = _n
egen education_max = max(yrseduc), by(hhid)
keep if n == 1


local descriptive_vars "assetindx education_max anglican catholic pentecostal christianother religionother"


clear matrix
local i = 1
foreach var in `descriptive_vars' {
	local i = `i'+1
	}
capture matrix S = J(17,`i'-1,.)
capture matrix Stars = J(17,`i'-1,0)
mat rownames S = `descriptive_vars'
local i = 1


forvalues v = 1/18 {
preserve
keep if villagenum == `v'
	quietly spatwmat, name(householddistance) xcoord(`x') ycoord (`y') band(0 1)

local i = 1
foreach var in `descriptive_vars' {
local j = 1
	if "`var'" == "logs" | "`var'" == "food" | "`var'" == "assets" {
	local i = `i' + 1
	}
	else if `v' <= 4{
	quietly spatgsa `var', weights(householddistance) moran
	matrix A = r(Moran)
	mat S[`v',`i'] = A[1,5]
	mat Stars[`v',`i'] = (A[1,5] < .1) + (A[1,5] < .05) + (A[1,5] < .01)
	local i = `i' + 1
	}
	else if `v' == 5{
	}
	else if `v' > 5{
	quietly spatgsa `var', weights(householddistance) moran
	matrix A = r(Moran)
	local v = `v' - 1
	mat S[`v',`i'] = A[1,5]
	mat Stars[`v',`i'] = (A[1,5] < .1) + (A[1,5] < .05) + (A[1,5] < .01)
	local i = `i' + 1
	local v = `v' + 1
	}
}
restore
}

frmttable using spatial_auto.tex, tex statmat(S) annotate(Stars) asymbol(*,**,***	) ///
replace sdec(3) ///
rtitle("Village 1"\"Village 2"\"Village 3"\"Village 4/5"\"Village 6"\"Village 7"\"Village 8"\"Village 9"\"Village 10"\"Village 11"\"Village 12"\"Village 13"\"Village 14"\"Village 15"\"Village 16"\"Village 17"\"Village 18") ///
ctitle("Village" "Assets"  "Education" "Anglican" "Catholic" "Pentecostal" "Christian (Other)" "Religion (Other)") ///
 fragment nocenter

